package com.example.wjy.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.superyi.myteam.R;
import com.example.room.entity.Team;
import com.example.wjy.TeamActivity;
import com.example.wjy.viewmodels.MyCreateTeamViewModel;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

/**
 * author: WangJunYan
 * package: com.example.wjy.adapter
 * date: 2020/6/3
 * time: 19:04
 * 显示“我创建的组队”的RecyclerView的适配器，在MyCreateTeamFragment中使用
 */
public class MyCreateTeamAdapter extends RecyclerView.Adapter<MyCreateTeamAdapter.MyCreateTeamViewHolder> {

    /**
     * A list of teams
     */
    List<Team> teamList;

    int []colors = {0x80FF4500,0x80FFB90F,0x8098F5FF,0x80836FFF,0x8066CD00,0x80B452CD};

    /**
     * A context from MyTeamActivity.
     * 用来测试每个item的点击事件是否能响应，实际上没用，只是为了使用Toast
     */
    Context context;

    public Context getContext() {
        return context;
    }

    MyCreateTeamViewModel myCreateTeamViewModel;

    public void setMyCreateTeamViewModel(MyCreateTeamViewModel myCreateTeamViewModel) {
        this.myCreateTeamViewModel = myCreateTeamViewModel;
    }



    /**
     * @param teamList The team list from MyCreateTeamViewModel.
     */
    public MyCreateTeamAdapter(List<Team> teamList) {
        this.teamList = teamList;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setTeamList(List<Team> teamList) {
        this.teamList = teamList;
    }

    @NonNull
    @Override
    public MyCreateTeamViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.team_item_layout, parent, false);

        return new MyCreateTeamViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyCreateTeamViewHolder holder, int position) {
        Team team = teamList.get(position);
        holder.teamName.setText(team.getTeamName());
        holder.teamType.setText(team.getTeamKind());
        holder.teamDirection.setText(team.getTeamIntro());
        holder.teamCount.setText(String.valueOf(team.getTeamNumOfAll()));
        holder.cv.setBackgroundColor(colors[position % 6]);
//        holder.teamId.setText(String.valueOf(team.getId()));

        //每个item的点击事件，可用于点击查看team具体信息，目前只是显示Toast
//        holder.itemView.setOnClickListener(v -> Toast.makeText(context,String.valueOf(team.getId()) , Toast.LENGTH_LONG).show());
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context,TeamActivity.class);
            intent.putExtra("name",team.getTeamName());
            intent.putExtra("kind",team.getTeamKind());
            intent.putExtra("numOfAll",String.valueOf(team.getTeamNumOfAll()));
            intent.putExtra("introduction",team.getTeamIntro());
            intent.putExtra("id",team.getTeamId());
            intent.putExtra("teamNumOfNow",team.getTeamNumOfNow());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
//        return teamList.size();
        return (teamList == null) ? 0 : teamList.size();
    }

    /**
     * 管理每个item的数据，其中的域目前并无实际含义，只是为了测试，可在布局文件修改后
     * 对应到数据库的具体字段
     */
    static class MyCreateTeamViewHolder extends RecyclerView.ViewHolder {

        TextView teamName;

        TextView teamType;

        TextView teamDirection;

        TextView teamCount;

        TextView teamId;

        CardView cv;
        public MyCreateTeamViewHolder(@NonNull View itemView) {
            super(itemView);
            teamName = itemView.findViewById(R.id.team_name);
            teamType = itemView.findViewById(R.id.team_kind);
            teamDirection = itemView.findViewById(R.id.team_intro);
            teamCount = itemView.findViewById(R.id.team_num_of_all);
            teamId = itemView.findViewById(R.id.team_id);
            cv = itemView.findViewById(R.id.team_item);
        }
    }
}


